﻿using MathNet.Numerics.Distributions;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using NetTaste;
using NPOI.SS.Formula.Functions;
using System;
using System.Text.Json;

namespace KAs.Cache;

public class DistributedCache : ICache
{
    private readonly IDistributedCache _cache;

    public DistributedCache(IDistributedCache cache)
    {
        _cache = cache;
    }
    public T Get<T>(string key)
    {
        var value = _cache.GetString(key);
        if (string.IsNullOrEmpty(value)) return default;
        return JsonSerializer.Deserialize<T>(value);
    }

    public async Task<string> GetAsync(string key)
    {
        var value = await _cache.GetStringAsync(key);
        return value;
    }

    public async Task<T> GetAsync<T>(string key)
    {
        var value = await _cache.GetStringAsync(key);
        if (string.IsNullOrEmpty(value)) return default;
        return JsonSerializer.Deserialize<T>(value);
    }

    public Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> func, TimeSpan expiredSeconds)
    {
        throw new NotImplementedException();
    }

    public void Refresh(string key, TimeSpan timeSpan)
    {
        _cache.Refresh(key);
    }

    public async Task RefreshAsync(string key, TimeSpan timeSpan)
    {
        await _cache.RefreshAsync(key);
    }

    public void Remove(string key)
    {
        _cache.Remove(key);
    }

    public async Task RemoveAsync(string key)
    {
        await _cache.RemoveAsync(key);
    }

    public void Set(string key, object value)
    {
        _cache.SetStringAsync(key, JsonSerializer.Serialize(value));
    }

    public void Set(string key, object value, TimeSpan timeSpan)
    {
        _cache.SetStringAsync(key, JsonSerializer.Serialize(value), new DistributedCacheEntryOptions().SetSlidingExpiration(timeSpan));
    }

    public async Task SetAsync(string key, string value)
    {
        await _cache.SetStringAsync(key, value);
    }

    public async Task SetAsync(string key, string value, TimeSpan timeSpan)
    {
        await _cache.SetStringAsync(key, value, new DistributedCacheEntryOptions().SetSlidingExpiration(timeSpan));
    }

    public async Task SetAsync(string key, object value)
    {
        await _cache.SetStringAsync(
                 key, JsonSerializer.Serialize(value)
             );
    }
    public async Task SetAsync(string key, object value, TimeSpan timeSpan)
    {
        await _cache.SetStringAsync(
                 key, JsonSerializer.Serialize(value),
                 new DistributedCacheEntryOptions().SetSlidingExpiration(timeSpan)
             );
    }

    public Task<double> StringDecrementAsync(string key, double count)
    {
        throw new NotImplementedException();
    }

    public Task<double> StringIncrementAsync(string key, double count)
    {
        throw new NotImplementedException();
    }

    public Task<int> StringIncrementAsync(string key, int count)
    {
        throw new NotImplementedException();
    }
}
